package com.gongpingjialoan.view;

import android.content.Context;
import android.support.v7.app.AppCompatDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.data.LoanData;

import java.util.List;

/**
 * 贷款方案dialog
 */
public class LoanDialog implements View.OnClickListener {

    private View dialog_close;
    private AppCompatDialog mDialog;
    private Context mContext;
    OnPickResultListener onPickResultListener;


    //贷款选项
    LinearLayout loanOtionsV;

    public LoanDialog(Context context) {
        this.mContext = context;
    }

    public void show(List<LoanData.DataBean.OptionsBean> options) {
        android.support.v7.app.AlertDialog.Builder dlg = new android.support.v7.app.AlertDialog.Builder(mContext, R.style.dialogs);
        View view = View.inflate(mContext, R.layout.loan_dialog, null);
        view.findViewById(R.id.apply_tv).setOnClickListener(this);
        loanOtionsV = (LinearLayout) view.findViewById(R.id.loan_opotions);
        bindLoadOptions(options);
        dlg.setView(view);
        mDialog = dlg.create();
//        mDialog.setCancelable(true);
//        mDialog.setCanceledOnTouchOutside(true);
        mDialog.show();
    }


    private void bindLoadOptions(List<LoanData.DataBean.OptionsBean> options) {
        loanOtionsV.removeAllViews();
        LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);

        for (int i = 0; i < options.size(); i++) {
            LoanData.DataBean.OptionsBean bean = options.get(i);
            View childV = mLayoutInflater.inflate(R.layout.item_loan_documents, null);
            loanOtionsV.addView(childV);
            TextView proportionT = (TextView) childV.findViewById(R.id.proportion);
            TextView month_payT = (TextView) childV.findViewById(R.id.month_pay);
            TextView monthsT = (TextView) childV.findViewById(R.id.months);
            TextView nameT = (TextView) childV.findViewById(R.id.name);
            TextView rateT = (TextView) childV.findViewById(R.id.rate);

            rateT.setText(bean.getRate());
            int advance = 0;
            try{
                advance = (int)Math.rint(Float.valueOf(bean.getAdvance()));
            }catch (NumberFormatException e){
                e.printStackTrace();
            }
            java.text.DecimalFormat df = new java.text.DecimalFormat("##,###,###");
            proportionT.setText(df.format(advance) + "元");

            proportionT.setText(bean.getProportion()+"车价");
            month_payT.setText(bean.getMonth_pay() + "元");
            monthsT.setText(bean.getMonths() + "个月");
            nameT.setText(bean.getName());

        }

    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.dialog_close:
                dismiss();
                break;
            case R.id.apply_tv:
                onPickResultListener.onResult();
                break;
        }
    }


    public interface OnPickResultListener {
        void onResult();
    }

    public void setOnPickResultListener(OnPickResultListener onPickResultListener) {
        this.onPickResultListener = onPickResultListener;
    }
}
